--
-- Author: shunguo.chen
-- Date: 2016-06-20 20:54:07
--
-- IMBoxPanel

local TabsBtn = require("app.widget.TabsBtn")
local ScrollViewExtend = require("app.widget.ScrollViewExtend")
local IconRender = require("app.widget.renders.IconRender")
local IMEmojiRender = import(".renders.IMEmojiRender")

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.IMChatBoxNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	display.nodeRemoveForNotHit(self, function()
			if self:isVisible() then
				self:performWithDelay(function() executeFunc(self._onNotHit) end, 0)
			end
		end)

	local btns = {}
	self._views = {}
	self._scrollViews = {}
	for i=1,7 do
		btns[i] = self._ui["selected_btn_"..i]
		self._ui["Text_"..i]:setString(L("chatShareList_"..i))
		self._views[i] = self._ui["ScrollView_"..i]
	end

	local function onTabsBtn(index)
		if self._tabIndex then
	    	self._tabIndex = index
	    	self:switchList()
	    else
	    	self._tabIndex = index
	    end
    end
	self._tabsBtn = TabsBtn.new(btns, onTabsBtn, 1, self._views)
end

function ClassRef:onBoxSwitch(cb)
	self._onBoxSwitch = cb
end

function ClassRef:onNotHit(cb)
	self._onNotHit = cb
end

--[[
--chatShareList_1			表情
--chatShareList_2			装备
--chatShareList_3			时装
--chatShareList_4			坐骑
--chatShareList_5			随从
--chatShareList_6			物品
--chatShareList_7			珠宝
-- ]]
function ClassRef:switchList(index)
	if index then self._tabIndex = index end

	if self._tabIndex == Enums.ImmPanelType.ImmPanel_Smile then
		if nil == self._scrollViews[self._tabIndex] then
			local itemSpr = self._views[self._tabIndex]:getChildByName("smile")
			itemSpr:setVisible(false)
			itemSpr:setTouchEnabled(true)
			local params = {
				class = IMEmojiRender,
				sprite = itemSpr
			}
			self._scrollViews[self._tabIndex] = ScrollViewExtend.create(self._views[self._tabIndex], params)
		end
		executeFunc(self._onBoxSwitch, self._tabIndex, self._scrollViews[self._tabIndex])
		self._scrollViews[self._tabIndex]:scrollToTop(0.1, true)
	else
		if nil == self._scrollViews[self._tabIndex] then
			local params = {
				class = IconRender,
				multipleLayerNum = 16,
			}
			self._views[self._tabIndex]:removeAllChildren()
			self._scrollViews[self._tabIndex] = ScrollViewExtend.create(self._views[self._tabIndex], params)
		end
		executeFunc(self._onBoxSwitch, self._tabIndex, self._scrollViews[self._tabIndex])
		self._scrollViews[self._tabIndex]:scrollToTop(0.1, true)
	end
end

function ClassRef:remove()
	for i = 1, 7 do
		if self._scrollViews[i] then
			self._scrollViews[i]:remove()
		end
	end
end

return ClassRef
